﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using GeneralDFS.Common;

namespace GeneralDFS.BusinessLogical.Nodes
{
    public partial class NameNode
    {
        private readonly BackgroundWorker _backgroundCheckHeartBeat = new BackgroundWorker();

        private void StartCheckHeartBeat()
        {
            _backgroundCheckHeartBeat.DoWork += backgroundCheckHeartBeat_DoWork;
            _backgroundCheckHeartBeat.WorkerSupportsCancellation = true;
            _backgroundCheckHeartBeat.RunWorkerAsync();
        }

        void backgroundCheckHeartBeat_DoWork(object sender, DoWorkEventArgs e)
        {
            var work = (BackgroundWorker) sender;
            while (!work.CancellationPending)
            {
                MasterNameNodeInfo = _nameNodeRemoteObject.FindSuccessor(DFSUtils.GetHash(Config.DetailConfig.HeartBeatHash));
                if (MasterNameNodeInfo.ID != LocalNode.ID)
                {
                    
                    //同步有效DataNode节点列表
                }

                MasterNameNodeRemoteObject = GetInstance(MasterNameNodeInfo);
                if(!IsInstanceValid(MasterNameNodeRemoteObject))
                    continue;

                //修改为配置
                Thread.Sleep(1000);
            }
        }

        private void StopCheckHeartBeat()
        {
            _backgroundCheckHeartBeat.CancelAsync();
        }
    }
}
